using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace GoobizFrame.Windows.Controls.TouchscreenKeyboard
{
    public partial class GKeyboard : UserControl
    {
        private Boolean shiftindicator = false;
        private Boolean capslockindicator = false;

        public GKeyboard()
        {
            InitializeComponent();
            //row1
            gKey_Quote.Click += new EventHandler(gKey_Click);            
            gKey_1.Click += new EventHandler(gKey_Click);
            gKey_2.Click += new EventHandler(gKey_Click);
            gKey_3.Click += new EventHandler(gKey_Click);
            gKey_4.Click += new EventHandler(gKey_Click);
            gKey_5.Click += new EventHandler(gKey_Click);
            gKey_6.Click += new EventHandler(gKey_Click);
            gKey_7.Click += new EventHandler(gKey_Click);
            gKey_8.Click += new EventHandler(gKey_Click);
            gKey_9.Click += new EventHandler(gKey_Click);
            gKey_0.Click += new EventHandler(gKey_Click);
            gKey_BS.Click += new EventHandler(gKey_Click);
            gKey13.Click += new EventHandler(gKey_Click);
            gKey14.Click += new EventHandler(gKey_Click);

            //row2
            gKey19.Click += new EventHandler(gKey_Click);
            gKey20.Click += new EventHandler(gKey_Click);
            gKey21.Click += new EventHandler(gKey_Click);
            gKey22.Click += new EventHandler(gKey_Click);
            gKey23.Click += new EventHandler(gKey_Click);
            gKey24.Click += new EventHandler(gKey_Click);
            gKey25.Click += new EventHandler(gKey_Click);
            gKey26.Click += new EventHandler(gKey_Click);
            gKey27.Click += new EventHandler(gKey_Click);
            gKey28.Click += new EventHandler(gKey_Click);
            gKey29.Click += new EventHandler(gKey_Click);
            gKey30.Click += new EventHandler(gKey_Click);
            gKey31.Click += new EventHandler(gKey_Click);
            gKey32.Click += new EventHandler(gKey_Click);

            //row 3
            gKey34.Click += new EventHandler(gKey_Click);
            gKey35.Click += new EventHandler(gKey_Click);
            gKey36.Click += new EventHandler(gKey_Click);
            gKey37.Click += new EventHandler(gKey_Click);
            gKey38.Click += new EventHandler(gKey_Click);
            gKey39.Click += new EventHandler(gKey_Click);
            gKey40.Click += new EventHandler(gKey_Click);
            gKey41.Click += new EventHandler(gKey_Click);
            gKey42.Click += new EventHandler(gKey_Click);
            gKey43.Click += new EventHandler(gKey_Click);
            gKey44.Click += new EventHandler(gKey_Click);
            gKey45.Click += new EventHandler(gKey_Click);

            //row 4
            gKey49.Click += new EventHandler(gKey_Click);
            gKey50.Click += new EventHandler(gKey_Click);
            gKey51.Click += new EventHandler(gKey_Click);
            gKey52.Click += new EventHandler(gKey_Click);
            gKey53.Click += new EventHandler(gKey_Click);
            gKey54.Click += new EventHandler(gKey_Click);
            gKey55.Click += new EventHandler(gKey_Click);
            gKey56.Click += new EventHandler(gKey_Click);
            gKey57.Click += new EventHandler(gKey_Click);
            gKey58.Click += new EventHandler(gKey_Click);
            gKey61.Click += new EventHandler(gKey_Click);
            gKey71.Click += new EventHandler(gKey_Click);
            gKey46.Click += new EventHandler(gKey_Click);
            
            //row 5
            gKey65.Click += new EventHandler(gKey_Click);
            gKey75.Click += new EventHandler(gKey_Click);
            gKey76.Click += new EventHandler(gKey_Click);
            gKey77.Click += new EventHandler(gKey_Click);

        }

        void gKey_Click(object sender, EventArgs e)
        {
            GKey gKey = (GKey)sender;
            KeyboardEventArgs pKeyboardEventArgs = null;
            string Keypressed = null;
            switch (gKey.Text)
            {
                //row 1
                case "ESC":
                    Keypressed = "{ESCAPE}";
                    break;
                case "`":
                case "~":
                case "1":
                case "!":
                case "2":
                case "@":
                case "3":
                case "#":
                case "4":
                case "$":
                case "5":
                case "%":
                case "6":
                case "^":
                case "7":
                case "&":
                case "8":
                case "*":
                case "9":
                case "(":
                case "0":
                case ")":
                case "-":
                case "_":
                case "=":
                case "+":
                    Keypressed = HandleShiftableKey(gKey.Text.ToLower());
                    break;
                case "[X]":
                    Keypressed = "{BACKSPACE}";
                    break;

               ////////////////////////////////////////////////////////////////////
                // row 2
                case "TAB":
                    Keypressed = "{TAB}";
                    break;
                case "Q":
                case "W":
                case "E":
                case "R":
                case "T":
                case "Y":
                case "U":
                case "I":
                case "O":
                case "P":
                case "[":
                case "{":
                case "]":
                case "}":
                case @"\":
                case @"|":
                   Keypressed = HandleShiftableKey(gKey.Text.ToLower());
                   break;
               case "DELETE":
                   Keypressed = "{DELETE}";
                   break;

               //////////////////////////////////////////////////////////////////////
                    //row 3
               case "A":
               case "S":
               case "D":
               case "F":
               case "G":
               case "H":
               case "J":
               case "K":
               case "L":
               case ";":
               case ":":
               case "'":               
                   Keypressed = HandleShiftableCaplockableKey(gKey.Text.ToLower());
                   break;
               case "CAPSLOCK":
                   Keypressed = "{CAPSLOCK}";
                   break;
               case "ENTER":
                   Keypressed = "{ENTER}";
                   break;
                    /////////////////////////////////////////////////////////////////////
                    //row 4
               case "Z":
               case "X":
               case "C":
               case "V":
               case "B":
               case "N":
               case "M":
               case ",":
               case "<":
               case ".":
               case ">":
               case "/":
               case "?":
                   Keypressed = HandleShiftableCaplockableKey(gKey.Text.ToLower());
                   break;
               case "UP":
                   Keypressed = "{UP}";
                   break;
               case "HOME":
                   Keypressed = "{Home}";
                   break;
               case "END":
                   Keypressed = "{End}";
                   break;
                    /////////////////////////////////////////////////////////////////////
                    //row 5
               case "SPACE":
                   Keypressed = " ";
                   break;               
               case "LEFT":
                   Keypressed = "{Left}";
                   break;
               case "DOWN":
                   Keypressed = "{Down}";
                   break;
               case "RIGHT":
                   Keypressed = "{Right}";
                   break;
            }

            if (Keypressed != null)
            {
                if(shiftindicator ) shiftindicator = !shiftindicator;
                pKeyboardEventArgs = new KeyboardEventArgs(Keypressed);
                OnUserKeyPressed(pKeyboardEventArgs);
            }
        }

        private string HandleShiftableCaplockableKey(string theKey)
        {

            if (capslockindicator)
            {
                return "+" + theKey;
            }
            else if (shiftindicator)
            {
                return "+" + theKey;
            }
            else
            {
                return theKey;
            }
        }
        private string HandleShiftableKey(string theKey)
        {
            if (shiftindicator)
            {
                return "+" + theKey;
            }
            else
            {
                return theKey;
            }
        }

        [Category("Mouse"), Description("Return value of mouseclicked key")]
        public event KeyboardDelegate UserKeyPressed;
        protected virtual void OnUserKeyPressed(KeyboardEventArgs e)
        {
            if (UserKeyPressed != null)
                UserKeyPressed(this, e);
        }

        private void gKey48_Click(object sender, EventArgs e)
        {
            shiftindicator = !shiftindicator;
        }

        private void gKey33_Click(object sender, EventArgs e)
        {
            capslockindicator = !capslockindicator;
        }

       
    }

    
}
